/**
 * Created by Administrator on 2017-7-27.
 */
import nattyStorage from 'natty-storage';
import Utils from './../utils';
import DB from './db';

const storage = nattyStorage({
  type: 'localStorage',  // 缓存方式, 默认为'localStorage'
  key: 'sskj-wechat',     // !!! 唯一必选的参数, 用于内部存储 !!!
  tag: 'v1.0.0.0',          // 缓存的标记, 用于判断是否有效
  duration: 1000 * 60 * 100,      // 缓存的有效期长, 以毫秒数指定
})

var StorageUtils = {

  getStorage(){
    return storage;
  },

  deleUserInfo(){
    storage.remove('user');
  },

  getLocalUserInfo(){

  },

  getBtInfo(){
    return storage.get('bt');
  },

  getUserInfo(){
    console.info("getUserInfo_1");
    if (!storage.get('user') || !storage.get('user').openId) {
      alert("用户为空，获取用户");
      const code = Utils.GetQueryString(window.location.search, 'code');
      DB.WechatAPI.getUser({code: code}).then(
        function (content) {
          storage.set('user', content);
          storage.get('bt', content);
          return content;
        },
        function (error) {
          console.log(error);

          return null;
        });
    } else {
      return storage.get('user');
    }
  },

  setWecharUserInfo(params){
    return storage.set('user', params);
  }

}



export default StorageUtils;
